<?php

namespace app\api\logic;
use app\common\basics\Logic;
use app\service\TechnWeixinService;
use app\service\UserWeixinService;
use think\Db;


/**
 * 工作台统计
 * Class StatLogic
 * @package app\admin\logic\index
 */
class SendMuLogic extends Logic
{
    public static function send_mu($send_openid,array $order_info){
        if (!empty($order_info)){
            $user_id = $order_info['user_id'];
//            if ($order_info['send_tong'] != 0){
//                dump('已发送过通知');
//                die;
//            }
            /////////////上方测试，下方正式/////////
            //查询订单详情
            $order_detail_title = Db::name('order_detail')->where(['order_id' => $order_info['id']])->find();
            //  $order_detail_title = implode(',', $order_detail_title);
            $order_detail_title_new='服务项目:'.implode(',',$order_detail_title);
            //  foreach ($order_detail_title as $k=>$v){
            //      $order_detail_title_new.="\n ".($k+1).'.'.$v;
            //  }
            //查询下单人
            $order_address = Db::name('order_address')->where(['order_id' => $order_info['id']])->find();
            $order_info = Db::name('order')->where('id',$order_info['id'])->find();
            $proxy_info = Db::name('proxy')->where('qu',$order_info['qu'])->where('is_xian',1)->find();
            $idd = $proxy_info['qu'];
            if (empty($proxy_info)){
                $proxy_info = Db::name('proxy')->where('shi',$order_info['shi'])->find();
                $idd = $proxy_info['shi'];

                $diqi = Db::name('area')->where('id',$idd)->value('short_name');
            }else{
                $diqi = Db::name('area')->where('id',$idd)->value('short_name');
            }
            $qianzhui = ' ';
            $shengji = Db::name('order_upgrade')->where('order_id',$order_info['id'])->find();
            $order_yuan = '¥'.$order_info['pay_price'].'元';
            if (!empty($shengji)){
                $qianzhui = ' - 升级';
                $shengji_price = Db::name('pay_log')->where('order_id',$order_info['id'])->order('add_time desc')->value('pay_price');
                $yuan_price = Db::name('pay_log')->where('order_id',$order_info['id'])->order('add_time asc')->value('pay_price');
                $order_yuan = '原¥'.$yuan_price.'元,补差价¥'.$shengji_price.'元';
                $order_detail_title = Db::name('order_updetail')->where('order_id',$order_info['id'])->find();
            }
            $jiazhong = Db::name('order_jzdetail')->where('order_id',$order_info['id'])->order('id desc')->find();
            if (!empty($jiazhong)){
                $qianzhui = ' - 加钟';
                $order_yuan = '¥'.$jiazhong['price']*$jiazhong['buy_number'].'元';
                $order_detail_title = $jiazhong;

            }
            //查询用户
            $user_info = Db::name('user')->where(['id' => $user_id])->find();
            $tem_msg['touser'] = $send_openid;
            $tem_msg['template_id'] = 'TU84vzZmOl8vnVG5SPKUCDWEZ1UAI0TkOheEm3QSCHs';
//            $tem_msg['miniprogram'] = [
//                'appid' => TechnWeixinService::$mini_appid,
//                'pagepath' => 'pages/order/orderdetail?order_id='.$order_id
//            ];


            if($order_info['budan_id']>0){
                $first='您好，有客户补单成功';
            }else{
                $first='您好，有客户下单成功';
            }

            $tem_msg['data'] = [
                'first' => ['value'=>$first],
                'keyword1' => [
                    'value' => '【'.$diqi.'】'.$qianzhui,
                ],
                'keyword2' => [
                    'value' => $order_detail_title['title'].'('.$order_detail_title['price'].')*'.$order_detail_title['buy_number'],
                ],
                'keyword3' => [
                    'value' => $user_info['mobile'].'-'.$user_info['nickname'],
                ],
                'keyword4' =>[
                    'value'=>$order_yuan,
                ],
                'keyword5' => [
                    'value'=>date('Y-m-d H:i',time())
                ],
                'remark' =>['value'=>'请前往系统页面查看'],
            ];
//            dump(json_encode($tem_msg,256));die;

            UserWeixinService::send_wx_web_tem_msg($tem_msg);
        }

    }

    public static function send_mu_refund($send_openid,$order_info,$order_detail_title,$proxy){
        $tem_msg['touser'] = $send_openid;
        $tem_msg['template_id'] = 'UNuia9wZ6kfFGTpELjv52KVTlpMmvmNVgz0GV12K0WA';
        $user = Db::name('user')->where('id',$order_info['user_id'])->find();

        $tem_msg['data'] = [
            'character_string1' => [
                'value' => $order_info['order_no'],
            ],
            'amount2' => [
                'value' => $order_info['refund_price'].'元',
            ],
            'thing6' => [
                'value' => $order_detail_title['title'],
            ],
            'time4' =>[
                'value'=>date('Y-m-d H:i:s',time()),
            ],
            'thing3' => [
                'value'=>'['.$proxy['user_name'].']-'.$user['nickname'].':'.$user['mobile']
            ],
        ];
//            dump(json_encode($tem_msg,256));die;

        $return = UserWeixinService::send_wx_web_tem_msg($tem_msg);
//        dump($return);
    }

    public static function send_mu_new($send_openid,array $order_info)
    {
        if (!empty($order_info)) {
            $user_id = $order_info['user_id'];
            $techn_id = $order_info['techn_id'];

            /////////////上方测试，下方正式/////////
            //查询订单详情
            $order_detail_title = Db::name('order_detail')->where(['order_id' => $order_info['id']])->find();
            //  $order_detail_title = implode(',', $order_detail_title);
            $order_detail_title_new = '服务项目:' . implode(',', $order_detail_title);
            //  foreach ($order_detail_title as $k=>$v){
            //      $order_detail_title_new.="\n ".($k+1).'.'.$v;
            //  }
            //查询下单人
            $order_address = Db::name('order_address')->where(['order_id' => $order_info['id']])->find();
            $order_info = Db::name('order')->where('id', $order_info['id'])->find();
            $proxy_info = Db::name('proxy')->where('qu', $order_info['qu'])->where('is_xian', 1)->find();
            $idd = $proxy_info['qu'];
            if (empty($proxy_info)) {
                $proxy_info = Db::name('proxy')->where('shi', $order_info['shi'])->find();
                $idd = $proxy_info['shi'];

                $diqi = Db::name('area')->where('id', $idd)->value('short_name').'-';
            } else {
                $diqi = Db::name('area')->where('id', $idd)->value('short_name').'-';
            }
            $qianzhui = '';
            $shengji = Db::name('order_upgrade')->where('order_id', $order_info['id'])->find();
            $order_yuan = '¥' . $order_info['pay_price'] . '元';
            if (!empty($shengji)) {
                $qianzhui = '升级-';
                $shengji_price = Db::name('pay_log')->where('order_id', $order_info['id'])->order('add_time desc')->value('pay_price');
                $yuan_price = Db::name('pay_log')->where('order_id', $order_info['id'])->order('add_time asc')->value('pay_price');
                $order_yuan = '补差价¥' . $shengji_price . '元';
                $order_detail_title = Db::name('order_updetail')->where('order_id', $order_info['id'])->find();
            }
            $jiazhong = Db::name('order_jzdetail')->where('order_id', $order_info['id'])->order('id desc')->find();
            if (!empty($jiazhong)) {
                $qianzhui = '加钟-';
                $order_yuan = '¥' . (int)$jiazhong['price'] * $jiazhong['buy_number'] . '元';
                $order_detail_title = $jiazhong;
            }
            //查询用户
            $user_info = Db::name('user')->where(['id' => $user_id])->find();
            $techn_info = Db::name('techn')->where(['id' => $techn_id])->find();
            $address_info = Db::name('order_address')->where('order_id', $order_info['id'])->find();
            $tem_msg['touser'] = $send_openid;
//            $tem_msg['template_id'] = 'TU84vzZmOl8vnVG5SPKUCDWEZ1UAI0TkOheEm3QSCHs';
            $tem_msg['template_id'] = 'OZydDe9SGdlIJ2UZbRplJEUBf2RfNkGckot-Gur-Kbo';
//            $tem_msg['miniprogram'] = [
//                'appid' => TechnWeixinService::$mini_appid,
//                'pagepath' => 'pages/order/orderdetail?order_id='.$order_id
//            ];


            if ($order_info['budan_id'] > 0) {
                $first = '您好，有客户补单成功';
            } else {
                $first = '您好，有客户下单成功';
            }

            $tem_msg['data'] = [
                'first' => ['value' => $first],
                'thing8' => [
                    'value' =>  $diqi . $qianzhui . $order_detail_title['title'] . (int)$order_detail_title['price'] . '*' . $order_detail_title['buy_number'],
                ],
                'thing16' => [
                    'value' => $user_info['mobile'] . '-' . $user_info['nickname'],
                ],
                'thing11' => [
                    'value' => $techn_info['nickname'],
                ],
                'thing5' => [
                    'value' =>  $address_info['county_name'] . $address_info['address'] ,
                ],
                'amount10' => [
                    'value' => $order_yuan,
                ],
                'remark' => ['value' => '请前往系统页面查看'],
            ];
//            dump(json_encode($tem_msg,256));die;

//            dump(UserWeixinService::send_wx_web_tem_msg($tem_msg));
            UserWeixinService::send_wx_web_tem_msg($tem_msg);
        }
    }


}